<template>
  <el-dialog
    title="下载任务"
    width="900px"
    :visible.sync="visible"
    :append-to-body="true">
    <el-form :inline="true" ref="form"  label-width="90px">
      <el-row type="flex">
        <el-col :span="35">
          <el-form-item label="文件名：" label-width="50">
            <el-input v-model="dataForm.filename" clearable></el-input>
          </el-form-item>
          <el-form-item label="下载日期：">
            <el-date-picker value-format="yyyy-MM-dd"  v-model="dataForm.createdate1" style="width: 140px;"></el-date-picker>&nbsp;&nbsp;至&nbsp;&nbsp;
            <el-date-picker value-format="yyyy-MM-dd"  v-model="dataForm.createdate2" style="width: 140px;"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getDataList(1)">查询</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      class="base-table base-table-disabled"
      stripe
      :data="dataList"
      v-loading="dataListLoading"
      @sort-change="changeSortable"
      style="width: 100%;" >
      <el-table-column
        type="index"
        align="center"
        label="序号"
        width="65"
      >
      </el-table-column>
      <el-table-column
        prop="filename"
        min-width="180"
        header-align="center"
        show-overflow-tooltip
        align="center"
        label="文件名">
      </el-table-column>
      <el-table-column
        prop="startdate"
        min-width="120"
        header-align="center"
        align="center"
        label="开始时间">
      </el-table-column>
      <el-table-column
        prop="createdate"
        min-width="120"
        header-align="center"
        align="center"
        label="完成时间">
      </el-table-column>
      <el-table-column
        prop="status"
        min-width="50"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <span style="color: red" v-if="scope.row.status=='失败'">{{scope.row.status}}</span>
          <span  v-else>{{scope.row.status}}</span>
        </template>
      </el-table-column>
      <el-table-column
        width="140"
        fixed="right"
        header-align="center"
        align="center"
        label="操作">
        <template slot-scope="scope">
          <el-button  type="text" size="small" :disabled="scope.row.status=='失败'"  @click="handleDownload(scope.row.miniourl)">下载</el-button>
          <el-button class="color-red" type="text" size="small" @click="handleDelete(scope.row)" >移除</el-button>
          <a style="color: white;" id="download"></a>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="base-pagination base-pagination-bg"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      ref="paginationData"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </el-dialog>
</template>

<script>
import { clearLoginInfo } from '@/utils'
export default {
  data () {
    return {
      visible: false,
      dataListLoading:false,
      picUrl: window.SITE_CONFIG['picUrl'],
      dataList:[],
      dataForm:{
        filename:'',
        createdate1:'',
        createdate2:'',
        sortType:'',
        sortName:'',
      },
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
    }
  },
 created() {
   const start = new Date();
   console.log("this.dateFormatter(start, false):",this.dateFormatter(start, false))
   this.dataForm.createdate1 = this.dateFormatter(start, false);
   this.dataForm.createdate2 = this.dateFormatter(start, false);
  },
  computed: {
    userName: {
      get () { return this.$store.state.user.name }
    },
    mainTabs: {
      get () { return this.$store.state.common.mainTabs },
      set (val) { this.$store.commit('common/updateMainTabs', val) }
    }
  },
  methods: {
    // 初始化
    init () {
      this.visible = true
      this.getDataList(1)
    },
    changeSortable(val) {
      if (val.order === 'ascending') {
        this.dataForm.sortType = 'asc'
        this.dataForm.sortName = val.prop
      } else if (val.order === 'descending') {
        this.dataForm.sortType = 'desc'
        this.dataForm.sortName = val.prop
      } else {
        this.dataForm.sortType = ''
        this.dataForm.sortName = ''
      }
      this.getDataList()
    },
    getDataList(id){
      if(id){
        this.pageIndex=id
        this.$nextTick(()=>{
          this.$refs.paginationData.lastEmittedPage=id
        })
      }
      this.dataListLoading=true;
      this.$http({
        url: this.$http.adornUrl('/ty/ty/getExcelfile'),
        method: 'get',
        params: this.$http.adornParams({
          'createdate1':this.dataForm.createdate1,
          'createdate2':this.dataForm.createdate2,
          'filename':this.dataForm.filename,
          'page': this.pageIndex,
          'limit': this.pageSize,
          'sortType': this.dataForm.sortType,
          'sortName': this.dataForm.sortName,
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPageSP = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    //下载
    handleDownload(filename) {
      this.$http({
        url: this.$http.adornUrl("/ty/ty/downloadAnnex"),
        method: "get",
        params: this.$http.adornParams({
          filename: encodeURIComponent(filename)
        })
      }).then(({ data }) => {
        console.log("附件下载结果：" + data);
        console.log("附件下载结果,路径：" + decodeURIComponent(data.url));
        if (data && data.code === 0) {
          console.log("下载完了");
          window.open(decodeURIComponent(data.url));
        } else {
          //this.$message.error("下载失败！");
        }
      });
    },
    // 删除
    handleDelete(row){
      this.$confirm('确定移除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/ty/ty/deleteExcelfile'),
          method: 'get',
          params: this.$http.adornParams(row)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.dataList.splice(this.dataList.indexOf(row),1) ;
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {

      });
    },
    download (filename) {
      document.getElementById("download").href = this.picUrl +'/tmp/' + filename;
      document.getElementById("download").click();
    },
  }
}
</script>

